package com.mchsdk.paysdk.activity;

import java.util.ArrayList;
import java.util.List;

import com.lidroid.xutils.BitmapUtils;
import com.mchsdk.paysdk.adapter.MCPacksAdapter;
import com.mchsdk.paysdk.bean.PersonalCenterModel;
import com.mchsdk.paysdk.bean.UserReLogin;
import com.mchsdk.paysdk.bean.UserReLogin.ReLoginCallback;
import com.mchsdk.paysdk.common.Constant;
import com.mchsdk.paysdk.dialog.MCTipDialog;
import com.mchsdk.paysdk.dialog.ReceivePackDialog;
import com.mchsdk.paysdk.entity.GamePackInfo;
import com.mchsdk.paysdk.entity.PackCodeEntity;
import com.mchsdk.paysdk.entity.PacksInfo;
import com.mchsdk.paysdk.http.process.GamePacksListProcess;
import com.mchsdk.paysdk.utils.BitmapHelp;
import com.mchsdk.paysdk.utils.MCHInflaterUtils;
import com.mchsdk.paysdk.utils.MCLog;
import com.mchsdk.paysdk.view.xlist.XListView;
import com.mchsdk.paysdk.view.xlist.XListView.IXListViewListener;

import android.annotation.SuppressLint;
import android.content.DialogInterface;
import android.content.DialogInterface.OnKeyListener;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class MCPacksActivity extends MCBaseActivity implements IXListViewListener {

    private static final String TAG = "MCPacksActivity";
    private XListView xListView;// 显示数据
    private MCPacksAdapter mPacksAdapter;//xlistview的适配器

    /**
     * 图片加载
     */
    BitmapUtils bitmapUtils;
    /**
     * 礼包列表
     */
    private List<GamePackInfo> packList = new ArrayList<GamePackInfo>();

    MCTipDialog packTip;//提示对话框

    private Handler mHandler = new Handler() {

        @SuppressLint("NewApi")
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case Constant.GET_PACKS_LIST_SUCCESS:
                    if (null != packTip) {
                        packTip.dismiss();
                    }
                    PacksInfo packsInfo = (PacksInfo) msg.obj;
                    if (null != packsInfo.getPackInfoList() && packsInfo.getPackInfoList().size() > 0) {
                        packList.addAll(packsInfo.getPackInfoList());
                        mPacksAdapter.notifyDataSetChanged();
                        xListView.invalidate();
                        xListView.setSelection(mPacksAdapter.getCount() - 1);
                    } else {
                        Toast.makeText(MCPacksActivity.this, "没有其它礼包了", Toast.LENGTH_LONG).show();
                    }
                    break;
                case Constant.GET_PACKS_LIST_FAIL:
                    if (null != packTip) {
                        packTip.dismiss();
                    }
                    MCLog.w(TAG, "error:" + (String) msg.obj);
                    Toast.makeText(MCPacksActivity.this, "没有其它礼包了", Toast.LENGTH_LONG).show();
                    break;
                case Constant.PACKS_CODE_FAIL:
                    if (null != packTip) {
                        packTip.dismiss();
                    }
                    String codeerror = (String) msg.obj;
                    Toast.makeText(MCPacksActivity.this, codeerror, Toast.LENGTH_LONG).show();
                    break;
                case Constant.PACKS_CODE_SUCCESS:
                    if (isDestroyed()) {
                        MCLog.e(TAG, "MCPacksActivity is destroyed!");
                        return;
                    }
                    PackCodeEntity packCode = (PackCodeEntity) msg.obj;
                    new ReceivePackDialog.Builder()
                            .setPackName(packCode.getPackName())
                            .setPackCode(packCode.getNovice())
                            .setPackStatus(packCode.getReceiveStatus())
                            .show(MCPacksActivity.this, getFragmentManager());
                    break;
                default:
                    if (null != packTip) {
                        packTip.dismiss();
                    }
                    break;
            }
        }

    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(MCHInflaterUtils.getLayout(this, "activity_mch_packs"));

        initview();
        initData();
    }

    private void initData() {
        if (TextUtils.isEmpty(PersonalCenterModel.getInstance().getUserId())) {

            UserReLogin reLogin = new UserReLogin(MCPacksActivity.this);
            reLogin.userToLogin(new ReLoginCallback() {

                @Override
                public void reLoginResult(boolean res) {
                    if (res) {
                        queryPacksList();
                    } else {
                        Toast.makeText(MCPacksActivity.this, "获取礼包失败,请登录", Toast.LENGTH_SHORT).show();
                        finish();
                    }
                }
            });
            return;
        }
        queryPacksList();
    }

    private void queryPacksList() {
        packTip = new MCTipDialog.Builder().setMessage("").show(this, this.getFragmentManager());

        new GamePacksListProcess().post(mHandler);
    }

    private void initview() {
        bitmapUtils = BitmapHelp.getBitmapUtils(getApplicationContext());

        TextView txtTitle = (TextView) findViewById(MCHInflaterUtils.getControl(this, "tv_mch_header_title"));
        txtTitle.setGravity(Gravity.CENTER | Gravity.CENTER_VERTICAL);
        txtTitle.setText("礼包中心");
        ImageView ivBack = (ImageView) findViewById(MCHInflaterUtils.getControl(this, "iv_mch_header_back"));
        ivBack.setVisibility(View.VISIBLE);
        ivBack.setOnClickListener(backClick);
        ImageView ivClose = (ImageView) findViewById(MCHInflaterUtils.getControl(this, "iv_mch_header_close"));
        ivClose.setVisibility(View.GONE);
//		ivClose.setOnClickListener(backClick);

        xListView = (XListView) findViewById(MCHInflaterUtils.getControl(this, "xlistview_mch_pack"));
        mPacksAdapter = new MCPacksAdapter(this, this, packList, bitmapUtils, mHandler);
        xListView.setAdapter(mPacksAdapter);

        xListView.setPullLoadEnable(false);
        xListView.setPullRefreshEnable(false);
        xListView.setXListViewListener(this);
        xListView.pullRefreshing();
        xListView.setDividerHeight(1);
    }

    /**
     * 返回按钮监听
     */
    OnKeyListener backKeyListener = new OnKeyListener() {

        @Override
        public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
            finish();
            return false;
        }
    };

    OnClickListener backClick = new OnClickListener() {

        @Override
        public void onClick(View v) {
            finish();
        }
    };

    @Override
    public void onRefresh() {
        new Handler().postDelayed(new Runnable() {

            @Override
            public void run() {
                xListView.stopRefresh();
            }
        }, 1000);

    }

    @Override
    public void onLoadMore() {
        // TODO Auto-generated method stub

    }

}
